package org.vshe.improve.spring.prospring3;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.context.Lifecycle;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;


/**
 * {@link Service} with hard-coded input data.
 */
@Component
public class DefaultService implements Service, SmartLifecycle {
	
	private boolean isRunning;
	
	@PostConstruct
	public void init() {
		isRunning  = true;
	}
	
	public String getMessage() {
		return "Hello world!";	
	}

	public boolean isRunning() {		
		return isRunning;
	}

	public void start() {
		System.out.println("Lifecyrcle start");		
	}

	public void stop() {
		System.out.println("Lifecyrcle stop");
		isRunning = false;
	}
	

	public int getPhase() {
		System.out.println("get phase");
		return 0;
	}

	public boolean isAutoStartup() {
		return true;
	}

	public void stop(Runnable callback) {
		System.out.println("Lifecyrcle stop with callback");
		isRunning = false;
		
		new Thread(callback).start();
		
	}
	
	@PreDestroy
	public void destroy() {
		System.out.println("DESTROY");
	}

}
